/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.ejbs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import project.entity.ListaAutor;
import project.entity.Submissao;

/**
 *
 * @author serafim
 */
@Stateless
public class ListaAutorFacade extends AbstractFacade<ListaAutor> {
    @PersistenceContext(unitName = "DAEPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ListaAutorFacade() {
        super(ListaAutor.class);
    }

    public int countPNome(String pNome, Submissao actual){
        Query cq = getEntityManager().createNamedQuery("searchPNome");
        cq.setParameter("pNome", pNome);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb.size();
    }
    
    public int countUNome(String uNome, Submissao actual){
        Query cq = getEntityManager().createNamedQuery("searchUNome");
        cq.setParameter("uNome", uNome);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb.size();
    }
    
    public int countEmail(String email, Submissao actual){
        Query cq = getEntityManager().createNamedQuery("searchEmail");
        cq.setParameter("email", email);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb.size();
    }
    
    public int countEmpresa(String empresa, Submissao actual){
        Query cq = getEntityManager().createNamedQuery("searchEmpresa");
        cq.setParameter("empresa", empresa);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb.size();
    }

    public List findRangePNome(int[] range, String pNome, Submissao actual) {
        Query cq = getEntityManager().createNamedQuery("searchPNome");
        cq.setParameter("pNome", pNome);
        cq.setFirstResult(range[0]);
        cq.setMaxResults(range[1] - range[0]);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb;
    }

    public List findRangeUNome(int[] range, String uNome, Submissao actual) {
        Query cq = getEntityManager().createNamedQuery("searchUNome");
        cq.setParameter("uNome", uNome);
        cq.setFirstResult(range[0]);
        cq.setMaxResults(range[1] - range[0]);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb;
    }
    
    public List findRangeEmail(int[] range, String email, Submissao actual) {
        Query cq = getEntityManager().createNamedQuery("searchEmail");
        cq.setParameter("email", email);
        cq.setFirstResult(range[0]);
        cq.setMaxResults(range[1] - range[0]);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb;
    }
    
    public List findRangeEmpresa(int[] range, String empresa, Submissao actual) {
        Query cq = getEntityManager().createNamedQuery("searchEmpresa");
        cq.setParameter("empresa", empresa);
        cq.setFirstResult(range[0]);
        cq.setMaxResults(range[1] - range[0]);
        List lb = cq.getResultList();
        verificaAutores(lb, actual);
        return lb;
    }

    private void verificaAutores(List lb, Submissao actual) {
        ArrayList<Integer> idx = new ArrayList<Integer>();
        for (Iterator it = lb.iterator(); it.hasNext();) {
            ListaAutor obj = (ListaAutor) it.next();
            for (Iterator it2 = obj.getSubmissoes().iterator(); it2.hasNext();){
                Submissao obj2 = (Submissao) it2.next();
                if (obj2.equals(actual)){
                    idx.add(lb.indexOf(obj));
                    break;
                }
            }
        }
        for (int i = idx.size() - 1; i >= 0; i--) {
            lb.remove(idx.get(i).intValue());
        }
    }
    
}
